<?php
$doc = new DOMDocument();
$doc->load("xml/alunos.xml");
$message = "";
$class = "";
$Alunos = $doc->getElementsByTagName("aluno");
    if($_POST != null){
        $doco = new DOMDocument();
        $doco->load("xml/chamada.xml");
        $chamadas = $doco->getElementsByTagName("chamadas");
        $chamada = $doco->createElement("chamada");
        $doco->appendChild($chamada);
                
        $data = $doco->createElement("data");
        $data->appendChild($doco->createTextNode($_POST['data']));
        $chamada->appendChild($data);
        
        $alunos = $doco->createElement("alunos");
        
        $codigos  = $_POST['ra'];
        $nomes  = $_POST['name'];
        $presencas  = $_POST['presenca'];
        for ($i = 0; $i < count($codigos); $i++) {
            $aluno =  $doco->createElement("aluno");
            
            $xmlra = $doco->createElement("ra");
            $xmlra->appendChild($doco->createTextNode($codigos[$i]));
            $aluno->appendChild($xmlra);
            
            $xmlnome = $doco->createElement("nome");
            $xmlnome ->appendChild($doco->createTextNode($nomes[$i]));
            $aluno->appendChild($xmlnome);
            
            $xmlpresenca = $doco->createElement("presenca");
            $xmlpresenca ->appendChild($doco->createTextNode($presencas[$i]));
            $aluno->appendChild($xmlpresenca);
            
            $alunos->appendChild($aluno);
        }
        $chamada->appendChild($alunos);
        $chamadas->item(0)->appendChild($chamada);
        if($doco->save("xml/chamada.xml") >0){
            $message = "Salvo com sucesso!";
            $class = "ok";
        }else{
            $message = "Não foi possivel lançar a chamada";
            $class = "error";
        }
    }
?>
<!DOCTYPE html>
<html>
    <head >
        <meta charset="utf-8">    
        <title> Chamada </title>
        <link href="css/estillo.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <div id="container">
            <div id="message" class="<?php echo $class;?>"><?php echo $message;?></div>
            <?php if(strlen($message)==0){?>
            <form method="POST" action="chamada.php">
                <table >
                    <tr>
                        <th>RA</th>
                        <th>Nome</th>
                        <th>Operação</th>
                    </tr>
                    <?php
                    foreach ($Alunos as $key => $value) {
                        $id = ($key % 2) ? "par" : "impar";
                        $ra = $value->getElementsByTagName("ra")->item(0)->nodeValue;
                        $name = $value->getElementsByTagName("nome")->item(0)->nodeValue;
                        ?>
                        <tr id='contato'class='<?php echo $id ?>'>
                            <td>
                                <input type="hidden" name="ra[]" value="<?php echo $ra ?>"/>
                                <?php echo $ra ?>
                            </td>
                            <td>
                                <input type="hidden" name="name[]" value="<?php echo $name ?>"/>
                                <?php echo $name ?>
                            </td>
                            <td>
                                <input type="number" name="presenca[]" value="" required="true" min="0" max="5"/>
                            </td>
                        </tr>
                        <?php
                    }
                    ?>
                </table>
                <fieldset>
                    <legend>Chamada</legend>               
                    <input type="date" name="data" required="true"/>
                    <input type="submit" value="Salvar"/>
                 </fieldset>
            </form>
            <?php }else{?>
            <a href="index.php">voltar</a>
             <?php }?>
        </div>
    </body>
</html>